package com.shark.demo;

import java.util.concurrent.*;

/**
 * @ClassName ThreadPool
 * @Description TODO
 * @Author 东北彭于晏
 * @Date 2024/9/30  17:54
 * @Version 1.0
 **/
public class ThreadPool {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2,
                3L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );


        try {
            for (int i = 1; i <= 1000; i++) {
                final int tmpI = i;
                threadPoolExecutor.submit(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 处理业务" + "\t 顾客: " + tmpI);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPoolExecutor.shutdown();
        }


    }
}
